package com.gitee.wsl.ext.string

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract


/**
 * @return Whether this string is valid
 *
 * Examples:
 * - null -> false
 * - "null" -> false
 * - "" -> false
 * - "NULL" -> false
 * - "  " -> false
 */
 @OptIn(ExperimentalContracts::class)
fun String?.isValid(): Boolean {
    contract {
        returns(true) implies (this@isValid != null)
    }
    return !isNullOrBlank() && !equals("null", true)
}

/**
 * Takes this string only if it [isValid]
 * @see isValid
 */
 fun String?.takeIfValid(): String? = if (isValid()) this else null


